package com.czl.module_login.activity

import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.style.AbsoluteSizeSpan
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.czl.base.base.AppManager
import com.czl.base.base.BaseActivity
import com.czl.base.config.AppConstants
import com.czl.base.push.PushHelper
import com.czl.base.route.RouteCenter
import com.czl.base.util.DialogHelper
import com.czl.base.util.TestHelper
import com.czl.module_login.BR
import com.czl.module_login.R
import com.czl.module_login.databinding.ActivitySplashBinding
import com.czl.module_login.viewmodel.SplashViewModel
import com.gyf.immersionbar.ImmersionBar
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

@Route(path = AppConstants.Router.Login.A_SPLASH)
class SplashActivity : BaseActivity<ActivitySplashBinding, SplashViewModel>() {
    private var isCheckPrivacy: Boolean = false

    override fun initContentView(): Int {
        return R.layout.activity_splash
    }

    override fun initParam() {
        ImmersionBar.hideStatusBar(window)
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        TestHelper.stop()
        initSplashBg()
        showPrivacy()
    }

    private fun initSplashBg() {
        Glide.with(this).load(
            R.mipmap.splash_img
        )
            .override(ScreenUtils.getAppScreenWidth(), ScreenUtils.getAppScreenHeight())
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .skipMemoryCache(true)
            .into(binding.ivSplash)
    }

    private fun showPrivacy() {
        isCheckPrivacy = dataRepository.getPrivacyState();
        if (!isCheckPrivacy) {
            val string = resources.getString(R.string.privacy_tips)
            val key1 = resources.getString(R.string.privacy_tips_key1)
            val key2 = resources.getString(R.string.privacy_tips_key2)
            val index1 = string.indexOf(key1)
            val index2 = string.indexOf(key2)

            //需要显示的字串

            //需要显示的字串
            val spannedString = SpannableString(string)
            //设置点击字体颜色
            val colorSpan1 = ForegroundColorSpan(resources.getColor(R.color.colorBlue))
            spannedString.setSpan(
                colorSpan1,
                index1,
                index1 + key1.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            val colorSpan2 = ForegroundColorSpan(resources.getColor(R.color.colorBlue))
            spannedString.setSpan(
                colorSpan2,
                index2,
                index2 + key2.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            //设置点击字体大小
            val sizeSpan1 = AbsoluteSizeSpan(18, true)
            spannedString.setSpan(
                sizeSpan1,
                index1,
                index1 + key1.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            val sizeSpan2 = AbsoluteSizeSpan(18, true)
            spannedString.setSpan(
                sizeSpan2,
                index2,
                index2 + key2.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            //设置点击事件
            val clickableSpan1: ClickableSpan = object : ClickableSpan() {
                override fun onClick(p0: View) {
                    startContainerActivity(AppConstants.Router.Web.F_WEB, Bundle().apply {
                        putString(
                            AppConstants.BundleKey.WEB_MENU_KEY,
                            AppConstants.Constants.AGREEMENT_TEXT_URL
                        )
                    })
                }

                override fun updateDrawState(ds: TextPaint) {
                    //点击事件去掉下划线
                    ds.isUnderlineText = false
                }
            }
            spannedString.setSpan(
                clickableSpan1,
                index1,
                index1 + key1.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )

            val clickableSpan2: ClickableSpan = object : ClickableSpan() {
                override fun onClick(p0: View) {
                    startContainerActivity(AppConstants.Router.Web.F_WEB, Bundle().apply {
                        putString(
                            AppConstants.BundleKey.WEB_MENU_KEY,
                            AppConstants.Constants.PRIVATE_TEXT_URL
                        )
                    })
                }

                override fun updateDrawState(ds: TextPaint) {
                    //点击事件去掉下划线
                    ds.isUnderlineText = false
                }
            }
            spannedString.setSpan(
                clickableSpan2,
                index2,
                index2 + key2.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            DialogHelper.showVersionDialog(this, spannedString, {
                dataRepository.savePrivacyState(true)
                PushHelper.init(applicationContext)
                toLogin()
            }, { AppManager.instance.appExit() })

        } else {
            toLogin()
        }
    }

    private fun toLogin() {
        viewModel.addSubscribe(
            Flowable.timer(1500L, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    if (viewModel.model.getLoginName().isNullOrBlank()) {
                        startContainerActivity(AppConstants.Router.Login.F_LOGIN)
                    } else {
                        RouteCenter.navigate(AppConstants.Router.Main.A_MAIN)
                    }
                    finish()
                })
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

}